package com.smart.community.commons.websocket;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;
import lombok.AllArgsConstructor;

/**
 * WebSocket消息类型枚举
 * 
 * @author Wu.Liang
 * @since 2025-01-30
 */
@Getter
@AllArgsConstructor
public enum WebSocketMessageType {

    // 系统消息类型
    CONNECT_SUCCESS("CONNECT_SUCCESS", "连接成功"),
    CONNECT_ERROR("CONNECT_ERROR", "连接错误"),
    DISCONNECT("DISCONNECT", "断开连接"),
    ERROR("ERROR", "错误消息"),
    PING("PING", "Ping消息"),
    PONG("PONG", "Pong响应"),
    HEARTBEAT("HEARTBEAT", "心跳消息"),
    HEARTBEAT_RESPONSE("HEARTBEAT_RESPONSE", "心跳响应"),

    // 房间管理消息类型
    JOIN_ROOM("JOIN_ROOM", "加入房间"),
    JOIN_ROOM_SUCCESS("JOIN_ROOM_SUCCESS", "加入房间成功"),
    LEAVE_ROOM("LEAVE_ROOM", "离开房间"),
    LEAVE_ROOM_SUCCESS("LEAVE_ROOM_SUCCESS", "离开房间成功"),

    // 聊天消息类型
    CHAT("CHAT", "聊天消息"),
    CHAT_PRIVATE("CHAT_PRIVATE", "私聊消息"),
    CHAT_ROOM("CHAT_ROOM", "房间聊天"),
    CHAT_BROADCAST("CHAT_BROADCAST", "广播消息"),

    // 通知消息类型
    NOTIFICATION("NOTIFICATION", "通知消息"),
    SYSTEM_NOTIFICATION("SYSTEM_NOTIFICATION", "系统通知"),
    PROPERTY_NOTIFICATION("PROPERTY_NOTIFICATION", "物业通知"),

    // 业务消息类型
    REPAIR_ORDER("REPAIR_ORDER", "报修工单"),
    COMPLAINT("COMPLAINT", "投诉建议"),
    VISITOR("VISITOR", "访客信息");

    @JsonValue
    private final String code;
    private final String description;

    /**
     * Jackson反序列化方法
     */
    @JsonCreator
    public static WebSocketMessageType fromCode(String code) {
        return getByCode(code);
    }

    /**
     * 根据code获取枚举
     */
    public static WebSocketMessageType getByCode(String code) {
        if (code == null) {
            return null;
        }
        for (WebSocketMessageType type : values()) {
            if (type.getCode().equals(code)) {
                return type;
            }
        }
        return null;
    }

    /**
     * 检查是否为系统消息类型
     */
    public static boolean isSystemMessage(String messageType) {
        return CONNECT_SUCCESS.getCode().equals(messageType) ||
               CONNECT_ERROR.getCode().equals(messageType) ||
               DISCONNECT.getCode().equals(messageType) ||
               ERROR.getCode().equals(messageType) ||
               PING.getCode().equals(messageType) ||
               PONG.getCode().equals(messageType) ||
               HEARTBEAT.getCode().equals(messageType) ||
               HEARTBEAT_RESPONSE.getCode().equals(messageType);
    }

    /**
     * 检查是否为聊天消息类型
     */
    public static boolean isChatMessage(String messageType) {
        return CHAT.getCode().equals(messageType) ||
               CHAT_PRIVATE.getCode().equals(messageType) ||
               CHAT_ROOM.getCode().equals(messageType) ||
               CHAT_BROADCAST.getCode().equals(messageType);
    }
}
